const Biz = require('../biz/index');
const fw = require('../core/framework');
const exception = require('../exception');

module.exports = {
    init(router,urlencodedParser,checkLogin,loginUsers){
        router.post('/alarmContactGroup/create', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                let cData = {
                    name:param.name,
                    merchant_id:fw.parseMerchantId(req)
                };
                Biz.AlarmContactGroup.create(cData,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            });
        });
        router.post('/alarmContactGroup/update', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                if(fw.isExit(param.id)){
                    Biz.AlarmContactGroup.update(param,(data)=>{
                        res.send(data);
                    },(data)=>{
                        res.send(data);
                    });
                }else{
                    res.send(exception.paramEx('id'));
                }
            });
        });
        router.post('/alarmContactGroup/delete', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                if(fw.isExit(param.id)){
                    Biz.AlarmContactGroupDevice.hasAlarmContactGroup({
                        alarm_contact_group_id:param.id
                    },(isUsedByDevice)=>{
                        if(isUsedByDevice){
                            res.send(exception['1411'])
                        }else{
                            Biz.AlarmContactGroupUser.hasAlarmContactGroup({
                                alarm_contact_group_id:param.id
                            },(isUsedByUser)=>{
                                if(isUsedByUser){
                                    res.send(exception['1412'])
                                }else{
                                    Biz.AlarmContactGroup.delete(param,(data)=>{
                                        res.send(data);
                                    },(data)=>{
                                        res.send(data);
                                    });
                                }
                            },(data)=>{
                                res.send(data);
                            });
                        }
                    },(data)=>{
                        res.send(data);
                    });

                }else{
                    res.send(exception.paramEx('id'));
                }
            });
        });
        router.post('/alarmContactGroup/search', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                param.merchant_id = fw.parseMerchantId(req);
                Biz.AlarmContactGroup.search(param,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            });
        });
        router.post('/enumeration/alarmContactGroup', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                param.merchant_id = fw.parseMerchantId(req);
                Biz.AlarmContactGroup.searchToEnumeration(param,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            });
        });
    }
};
